package yunquan.wang.modules.customer.web;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import yunquan.wang.base.dao.PropertyFilter;
import yunquan.wang.base.web.WebConstants;
import yunquan.wang.base.web.WebHelper;
import yunquan.wang.base.web.ajax.AjaxDone;
import yunquan.wang.modules.customer.entity.Contacts;
import yunquan.wang.modules.customer.entity.Customer;
import yunquan.wang.modules.customer.service.CustomerService;

@Controller
@RequestMapping("/customer/customer")
public class CustomerController{

	@Resource
	private CustomerService service;
	@RequestMapping(value = "/list.do")
	public String list(HttpServletRequest request, Model model) {
		Pageable page = WebHelper.getPage(request);
		List<PropertyFilter> filters = WebHelper.filterRequest(request);
		model.addAttribute(WebConstants.ATTRIBUTE_PAGE,service.findPage(page, filters));
		return "customer/customerList";
	}
	
	@RequestMapping(value = "/save.do")
	public @ResponseBody AjaxDone save(@Valid Customer entity, BindingResult bindingResult,	HttpServletRequest request) {
	    
		Set<Contacts> contancts=buildContacts(request);
		if (bindingResult.hasErrors()) {
			return WebHelper.validFail(bindingResult);
		} else {
			try {
				entity = service.save(entity,contancts);
			} catch (Exception e) {
			return	WebHelper.saveFail( e);
			}
		}
		return WebHelper.saveSuccess();
	}
	 
	private Set<Contacts> buildContacts(HttpServletRequest request) {
		Set<Contacts> contactsList=new HashSet<Contacts>();
	String names[]=	request.getParameterValues("contactsname");
	if(names!=null&&names.length>0)
	{
		String contactssex[]=	request.getParameterValues("contactssex");
		String contactspisition[]=	request.getParameterValues("contactspisition");
		String contactsage[]=	request.getParameterValues("contactsage");
		String contactsworkPhone[]=	request.getParameterValues("contactsworkPhone");
		String contactshomePhone[]=	request.getParameterValues("contactshomePhone");
		
		String contactsworkAddress[]=	request.getParameterValues("contactsworkAddress");
		String contactshomeAddress[]=	request.getParameterValues("contactshomeAddress");
		String contactsemail[]=	request.getParameterValues("contactsemail");
		
		for(int i=0;i<names.length;i++)
		{
			Contacts contacts=new Contacts();
			contacts.setName(names[i]);
			Boolean sex=contactssex[i]==null?null:Boolean.valueOf(contactssex[i]);
			contacts.setSex(sex);
			
			contacts.setPisition(contactspisition[i]);
			Integer age=contactsage[i]==null?null:Integer.valueOf(contactsage[i]);
			contacts.setAge(age);
			contacts.setWorkPhone(contactsworkPhone[i]);
			contacts.setHomePhone(contactshomePhone[i]);
			contacts.setWorkAddress(contactsworkAddress[i]);
			contacts.setHomeAddress(contactshomeAddress[i]);
			contacts.setEmail(contactsemail[i]);
			contactsList.add(contacts);
		}
	
	 
	}
	return contactsList;
	}

	@RequestMapping(value = "/view.do")
	public String view(String id,  Model model) {
		model.addAttribute(WebConstants.ATTRIBUTE_ENTITY, service.get(id));
		return "customer/customerView";
	}
	 
	@RequestMapping(value = "/input.do")
	public String input(String id,Model model) {
		Customer entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = service.get(id);
		} else {
			entity = new Customer();
		}
		model.addAttribute(WebConstants.ATTRIBUTE_ENTITY, entity);
		return "customer/customerInput";
	}
	
	@RequestMapping(value = "/delete.do")
	public @ResponseBody AjaxDone delete(String [] id) {
		for (String ids : id) {
			service.delete(ids);
		}
		return WebHelper.deleteSuccess();
	}
 
 
}
